package novel.front.novellist.test;

import com.google.gson.*;
import novel.front.novellist.entity.LiebaoApp;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sutong on 2018/6/17.
 */

public class UdApp {

    public static void main(String[] args) {
        List<String> listUrl = getUrlList();
        for (int i = 0; i < listUrl.size(); i++) {
            String jsonStr = getStrByUrl(listUrl.get(i));
            List<LiebaoApp> appList = jsonStrToObj(jsonStr);
        }
    }



    /**
     * 获取url
     *
     * @return
     */
    public static List<String> getUrlList() {
        List<String> list = new ArrayList<>();
        for (int i = 1; i < 100; i++) {
            list.add("http://xcx.9.cn/ajax/getapplist?page=" + i + "&code=youxi&status=1&order=");
        }
        return list;
    }

    /**
     * json转javabean
     *
     * @return
     */
    public static List<LiebaoApp> jsonStrToObj(String jsonStr) {
//        List<LiebaoApp> appList = JSON.parseObject(jsonStr, new TypeReference<ArrayList<LiebaoApp>>() {
//        });
//        return appList;
        List<LiebaoApp> liebaoJsonPojoList = new ArrayList<>();
        JsonParser jsonParser=new JsonParser();
        JsonObject result=jsonParser.parse(jsonStr).getAsJsonObject();
        LiebaoApp liebao;
        JsonArray jsonArray = result.get("data").getAsJsonObject().get("rows").getAsJsonArray();
        for (int i = 0; i < jsonArray.size(); i++) {
            liebao = new LiebaoApp();
            JsonObject obj = (JsonObject) jsonArray.get(i);
            liebao.setAppId(obj.get("id").getAsInt());
            liebao.setAppId(obj.get("uid").getAsInt());
            liebao.setAppSum(obj.get("content").getAsString());
            liebao.setDesc(obj.get("content").getAsString());
            liebao.setAppName(obj.get("title").getAsString());
            liebao.setAuthor("酷酷屋小程序");
//            liebao.setHot(obj.getString("visit_amount"));
            liebao.setLogoUrl(obj.get("icon").getAsString());
            liebao.setQrUrl(obj.get("qrcode").getAsString());
            liebao.setScrUrlsArr(obj.get("attr_imgs").getAsString());
            JsonArray tagArr = obj.get("tags").getAsJsonArray();
            String tagStr = "";
            for (int k = 0; k < tagArr.size(); k++) {
                String tagObj = tagArr.get(k).getAsString();
                tagStr += tagObj + ",";
            }
            liebao.setTagsArr(tagStr.substring(0, tagStr.length() - 1));
            liebao.setSourceId(9);
            liebao.setIsCheck(1);
            liebao.setSourceUrl("xcx.9.cn");
            liebao.setAppUpdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
            liebao.setCategory(20);
            liebao.setHomeType(20);
            liebaoJsonPojoList.add(liebao);
        }
        return liebaoJsonPojoList;
    }

    /**
     * HttpURLConnection Get方式获取数据
     *
     * @param urlStr
     * @return
     */
    public static String getStrByUrl(String urlStr) {
        try {
            long start = System.currentTimeMillis();
            URL url = new URL(urlStr);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//            connection.addRequestProperty("apikey", "0fc807e45a37ce264f45d169646f4a9e");
            long end = System.currentTimeMillis();
            System.out.println("timeGap:" + (end - start));
            return inputToString(connection.getInputStream(), "utf-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    /**
     * inputstream转string
     *
     * @param in
     * @param charset
     * @return
     */
    public static String inputToString(InputStream in, String charset) {
        byte[] buf = new byte[1024];
        StringBuffer sb = new StringBuffer();
        int len = 0;
        try {
            while ((len = in.read(buf)) != -1) {
                sb.append(new String(buf, 0, len, charset));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

}
